package cc.eddic.pratice.programmingparadigms.emp01array.impl;

import cc.eddic.pratice.programmingparadigms.Employee;
import cc.eddic.pratice.programmingparadigms.Result;
import cc.eddic.pratice.programmingparadigms.emp01array.EmployeeStatisticsService;

/**
 * 王巧家实现的版本
 */
public class StatisticsWqjImpl extends ReaderGcsImpl implements EmployeeStatisticsService {
    public Result[] pivotByGender(Employee[] employees){
        int index = 0;
        Result[] results = new Result[employees.length];
        for (Employee employee : employees){
            boolean isFound = false;
            for (Result result : results){
                if (null == result){
                    break;
                }
                if (result.getKey().equals(employee.getGender())){
                    isFound = true;
                    result.setCount(result.getCount() + 1);
                }
            }
            if (!isFound){
                results [index] = new Result(employee.getGender(), 1);
                index ++;
            }
        };
        Result[] newResults = new Result[index];
        for (int i = 0; i < index; i++){
            newResults[i] = results[i];
        }
        Reverse(newResults);
        return  newResults;
    }


    /**
     * 按照部门统计
     * @param employees Employee 数组
     * @return 统计结果数组
     */
    public Result[] pivotByDepartment(Employee[] employees){
        int index = 0;
        Result[] results = new Result[employees.length];
        for (Employee employee : employees){
            boolean isFound = false;
            for (Result result : results){
                if (null == result){
                    break;
                }
                if (result.getKey().equals(employee.getDepartment())){
                    isFound = true;
                    result.setCount(result.getCount() + 1);
                }
            }
            if (!isFound){
                results [index] = new Result(employee.getDepartment(), 1);
                index ++;
            }
        };
        Result[] newResults = new Result[index];
        for (int i = 0; i < index; i++){
            newResults[i] = results[i];
        }
        Reverse(newResults);
        return  newResults;
    }
    /**
     * 按照职位统计
     * @param employees Employee 数组
     * @return 统计结果数组
     */

    public Result[] pivotByJobTitle(Employee[] employees){
        int index = 0;
        Result[] results = new Result[employees.length];
        for (Employee employee : employees){
            boolean isFound = false;
            for (Result result : results){
                if (null == result){
                    break;
                }
                if (result.getKey().equals(employee.getJobTitle())){
                    isFound = true;
                    result.setCount(result.getCount() + 1);
                }
            }
            if (!isFound){
                results [index] = new Result(employee.getJobTitle(), 1);
                index ++;
            }
        };
        Result[] newResults = new Result[index];
        for (int i = 0; i < index; i++){
            newResults[i] = results[i];
        }
        Reverse(newResults);
        return  newResults;
    }

    private void Reverse(Result[] results){
        for (int i = results.length - 1; i > 0; i--){
            for (int j = results.length - 1; j >results.length - 1 - i; j--){
                if (results[j].getCount() > results[j - 1].getCount()){
                    Result result = results[j];
                    results[j] = results[j-1];
                    results[j-1] = result;
                }
            }
        }
    }


}